package org.nbict.iot.trident.influx.state;

import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.state.State;
import org.apache.storm.trident.tuple.TridentTuple;
import org.influxdb.dto.Point;
import org.nbict.iot.persist.pool.DbRepository;
import org.nbict.iot.persist.repo.InfluxRepo;
import org.nbict.iot.trident.influx.mapper.InfluxdbMapper;

import java.util.List;

/**
 * Created by songseven on 18/7/3.
 */
public class InfluxdbState implements State {


    private InfluxdbMapper mapper;

    public InfluxdbState(InfluxdbMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void beginCommit(Long aLong) {

    }

    @Override
    public void commit(Long aLong) {

    }

    public InfluxdbState() {

    }

    public void updateState(List<TridentTuple> tuples, TridentCollector
            collector) {
        //System.out.println("tuples size: " + tuples.size());
        tuples.forEach(tuple -> {
            Point point = mapper.toPoint(tuple);
            ((InfluxRepo) DbRepository.INFLUX.getInstance())
                    .insert(point);
        });
    }
}
